package com.ruoyi.workbench.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.workbench.domain.WbVisitLog;
import com.ruoyi.workbench.domain.WbWholeUnitVisitLogNew;
import com.ruoyi.workbench.service.IWbVisitLogService;
import com.ruoyi.workbench.utils.DateEntity;
import com.ruoyi.workbench.utils.Generator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 拜访日志Controller
 *
 * @author hanjiafeng
 * @date 2021-02-25
 */
@Controller
@RequestMapping("/workbench/visit_log")
public class WbVisitLogController extends BaseController {
    private final String prefix = "workbench/visit_log";
    private final IWbVisitLogService wbVisitLogService;

    public WbVisitLogController(IWbVisitLogService wbVisitLogService) {
        this.wbVisitLogService = wbVisitLogService;
    }

    @RequiresPermissions("workbench:visit_log:view")
    @GetMapping()
    public String visitLog() {
        return prefix + "/visit_log";
    }

    /**
     * 查询拜访日志列表
     */
    @RequiresPermissions("workbench:visit_log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WbWholeUnitVisitLogNew wbWholeUnitVisitLogNew, HttpServletRequest httpServletRequest) {
        startPage();
        wbWholeUnitVisitLogNew.setWbWholeUnitNew(Generator.generateWbWholeUnitNew(httpServletRequest));
        DateEntity dateEntity = Generator.initDateUtil(httpServletRequest);
        String types = httpServletRequest.getParameter("types");
        String[] typeArray = StringUtils.isEmpty(types) ? null : types.split(",");

        List<WbWholeUnitVisitLogNew> list = wbVisitLogService.selectWbWholeUnitVisitLogNewListDuringTime(wbWholeUnitVisitLogNew, dateEntity.getPeriod(), dateEntity.getStartTimeStr(), dateEntity.getEndTimeStr(), typeArray);
        return getDataTable(list);
    }

    /**
     * 导出拜访日志列表
     */
    @RequiresPermissions("workbench:visit_log:export")
    @Log(title = "信息上报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WbWholeUnitVisitLogNew wbWholeUnitVisitLogNew, HttpServletRequest httpServletRequest) {
        wbWholeUnitVisitLogNew.setWbWholeUnitNew(Generator.generateWbWholeUnitNew(httpServletRequest));
        DateEntity dateEntity = Generator.initDateUtil(httpServletRequest);
        String types = httpServletRequest.getParameter("types");
        String[] typeArray = StringUtils.isEmpty(types) ? null : types.split(",");

        List<WbWholeUnitVisitLogNew> list = wbVisitLogService.selectWbWholeUnitVisitLogNewListDuringTime(wbWholeUnitVisitLogNew, dateEntity.getPeriod(), dateEntity.getStartTimeStr(), dateEntity.getEndTimeStr(), typeArray);
        ExcelUtil<WbWholeUnitVisitLogNew> util = new ExcelUtil<>(WbWholeUnitVisitLogNew.class);
        return util.exportExcel(list, "export", ShiroUtils.getLoginName());
    }

    /**
     * 新增拜访日志
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存拜访日志
     */
    @RequiresPermissions("workbench:visit_log:add")
    @Log(title = "信息上报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WbVisitLog wbVisitLog) {
        return toAjax(wbVisitLogService.insertWbVisitLog(wbVisitLog));
    }

    /**
     * 修改拜访日志
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap modelMap) {
        WbVisitLog wbVisitLog = wbVisitLogService.selectWbVisitLogById(id);
        modelMap.put("wbWholeVisitLog", wbVisitLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存拜访日志
     */
    @RequiresPermissions("workbench:visit_log:edit")
    @Log(title = "信息上报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WbVisitLog wbVisitLog) {
        String screenShot = wbVisitLog.getScenePhoto();
        screenShot = StringUtils.isNull(screenShot) ? null : screenShot;
        wbVisitLog.setScenePhoto(screenShot);
        return toAjax(wbVisitLogService.updateWbVisitLog(wbVisitLog));
    }

    /**
     * 删除拜访日志
     */
    @RequiresPermissions("workbench:visit_log:remove")
    @Log(title = "信息上报", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wbVisitLogService.deleteWbVisitLogByIds(ids));
    }

    @PostMapping("/checkPhoneNumberUnique")
    @ResponseBody
    public String checkPhoneNumberUnique(Long id, String type, String phoneNumber) {
        return wbVisitLogService.checkPhoneNumberUnique(id, type, phoneNumber);
    }
}
